package com.wf.demo.job.service;

import com.wf.demo.info.dao.DeviceDAO;
import com.wf.demo.info.pojo.Company;
import com.wf.demo.info.pojo.Device;
import com.wf.demo.job.dao.DeviceNumberChangeOfCompanyDAO;
import com.wf.demo.job.pojo.DeviceNumberChangeOfCompany;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: wf
 * @Date: 2019/12/12 12:46
 * @Description:
 */
@Service
public class DeviceNumberChangeOfCompanyService {
    @Autowired
    DeviceNumberChangeOfCompanyDAO deviceNumberChangeOfCompanyDAO;
    @Autowired
    DeviceDAO deviceDAO;
    public void add( DeviceNumberChangeOfCompany bean){
        deviceNumberChangeOfCompanyDAO.save(bean);
    }
    public void update( DeviceNumberChangeOfCompany bean){
        deviceNumberChangeOfCompanyDAO.save(bean);
    }
    public void delete(int id){
        deviceNumberChangeOfCompanyDAO.delete(id);
    }
    public  DeviceNumberChangeOfCompany get(int id){
        return deviceNumberChangeOfCompanyDAO.findOne(id);
    }


    /**
     *功能描述   根据输入起始时间和结束时间筛选
     * @author wf
     * @date 2019/12/14
     * @param startTime
     * @param endTime
     * @return java.util.List<com.wf.demo.job.pojo.DeviceNumberChangeOfCompany>
     */
    public int searchCompanyMonthData(LocalDateTime startTime,LocalDateTime endTime,Company company){
        Sort sort = new Sort(Sort.Direction.DESC,"id");
        int numberChangeOfMonth= 0; //月设备数量变化量
        Specification<DeviceNumberChangeOfCompany> specification= new Specification<DeviceNumberChangeOfCompany>() {
            @Override
            public Predicate toPredicate(Root<DeviceNumberChangeOfCompany> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList=new ArrayList<Predicate>();  //predicate语句集合

                Path<LocalDateTime> localDateTimePath=root.get("creatTime");
                Predicate predicateTime=cb.between(localDateTimePath,startTime,endTime);
                Path<Company> companyPath=root.get("company");
                Predicate predicateCompany=cb.equal(companyPath,company);

                predicateList.add(predicateTime);
                predicateList.add(predicateCompany);
                Predicate endPredicate = cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
                return endPredicate;
            }
        };
        List<DeviceNumberChangeOfCompany> deviceNumberChangeOfCompanyList=deviceNumberChangeOfCompanyDAO.findAll(specification,sort);

        if(deviceNumberChangeOfCompanyList.isEmpty()){
            numberChangeOfMonth=99999;
            //99999，如果为月变化情况为99999说明当月数据为空，返回的DeviceNumberChangeOfCompany 对象全是null对象
            return numberChangeOfMonth;
        }
        for(DeviceNumberChangeOfCompany dncc :deviceNumberChangeOfCompanyList){
            if(null !=dncc){//判断数据不为空
                numberChangeOfMonth=numberChangeOfMonth+dncc.getNumberChangeDevice();
            }
        }
        return numberChangeOfMonth;
    }


}
